﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyCommerce.Business.Stores;
using Innova.Microfinanzas.Business.UsersAndSecurity;

namespace MyCommerce.Presentation.Stores.Presenters
{
    public class StorePresenter
    {
        private IViews.IStore vManagedView;

        public StorePresenter(IViews.IStore managedView)
        {
            if (managedView == null)
            {
                throw new Exception("managedView cannot be null");
            }

            vManagedView = managedView;
            vManagedView.OnLoad += new EventHandler(vManagedView_OnLoad);
            vManagedView.OnEdit += new IViews.OnEntityEventHandler(vManagedView_OnEdit);
            vManagedView.OnDelete += new IViews.OnEntityEventHandler(vManagedView_OnDelete);
            vManagedView.OnSave += new IViews.OnSaveEventHandler(vManagedView_OnSave);

        }

        void vManagedView_OnSave(object sender, bool isNew)
        {
            StoreManager vStoreManager = new StoreManager();
            if (isNew)
            {
                vStoreManager.SaveStore(vManagedView.objStore);
            }
            else
            {
                vStoreManager.UpdateStore(vManagedView.objStore);
            }
            vManagedView.colStore = vStoreManager.GetAllStores();
        }

        void vManagedView_OnDelete(object sender, long id)
        {
            StoreManager vStoreManager = new StoreManager();
            vStoreManager.DeleteStore(id);
            vManagedView.colStore = vStoreManager.GetAllStores();
        }

        void vManagedView_OnEdit(object sender, long id)
        {
            StoreManager vStoreManager = new StoreManager();
            vManagedView.objStore = vStoreManager.GetStoreByID(id);
        }

        void vManagedView_OnLoad(object sender, EventArgs e)
        {
            StoreManager vStoreManager = new StoreManager();
            EmployeeManager vEmployeeManager = new EmployeeManager();
            vManagedView.colStore = vStoreManager.GetAllStores();
            vManagedView.colEmployee = vEmployeeManager.GetEmployeeActives();
        }

    }
}
